//
//  ObjectMapper+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit
import ObjectMapper

// String Date 转换
open class DateStringTransform_y: TransformType {
    
    public typealias Object = Date
    public typealias JSON = String
    
    public func transformFromJSON(_ value: Any?) -> Date? {
        if let timeInt = value as? Double {
            guard timeInt > 0 else { return nil }
            return Date(timeIntervalSince1970: TimeInterval(timeInt / 1000))
        }
        
        if let timeStr = value as? String, !timeStr.isEmpty {
            guard timeStr != "0" else { return nil }
            return Date(timeIntervalSince1970: TimeInterval(atof(timeStr) / 1000))
        }
        
        return nil
    }
    
    public func transformToJSON(_ value: Date?) -> String? {
        if let date = value {
            return String(Int64(date.timeIntervalSince1970 * 1000))
        }
        return nil
    }
}

// String Int 转换
open class IntStringTransform_y: TransformType {
    
    public typealias Object = Int
    public typealias JSON = String
    
    public func transformFromJSON(_ value: Any?) -> Int? {
        if let val = value as? Int {
            return val
        }
        return nil
    }
    
    public func transformToJSON(_ value: Int?) -> String? {
        if let val = value {
            return String(val)
        }
        return nil
    }
}
